﻿@page "/finance"
@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject NotificationService NotificationService
@inject IFinanceService FinanceService 

<RadzenText TextStyle="TextStyle.H4">收支费用</RadzenText>
<PageTitle>收支费用</PageTitle>
<RadzenStack Style="display: flex; margin: 0px 10px 10px 0px" 
Orientation="Orientation.Horizontal" 
JustifyContent="JustifyContent.Start" 
AlignItems="AlignItems.Center">
    <RadzenRadioButtonList @bind-Value=@value TValue="int" Change="@((args) => Changed(args))">
        <Items>
            <RadzenRadioButtonListItem Text="全部" Value="0" />
            <RadzenRadioButtonListItem Text="收入" Value="1" />
            <RadzenRadioButtonListItem Text="支出" Value="2" />
        </Items>
    </RadzenRadioButtonList>
    <RadzenDatePicker DateFormat="yyyy-MM-dd" @bind-Value=@startDatetime @onchange:preventDefault Name="RadzenDatePickerBindValue" ShowCalendarWeek />
    <RadzenDatePicker DateFormat="yyyy-MM-dd" @bind-Value=@endDatetime @onchange:preventDefault Name="RadzenDatePickerBindValue" ShowCalendarWeek />
    <RadzenTextBox @bind-Value="@keyword" Placeholder="项目" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="查询" Icon="refresh" @onclick:stopPropagation Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" />
    <RadzenButton Text="记一笔" Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" />
</RadzenStack>

<RadzenDataGrid EmptyText="暂无数据" Data="@data" GridLines="@Radzen.DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="Id" Title="序号" Width="100px" TextAlign="TextAlign.Center" />
        <RadzenDataGridColumn Property="" Title="操作" Width="260px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Text="修改" @onclick:stopPropagation Click="@(args=> Update(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Text="删除" @onclick:stopPropagation Click="@(args=> Delete(data))" ButtonStyle="ButtonStyle.Danger" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="CreatedAt" Title="费用日期" />
        <RadzenDataGridColumn Property="FormType" Title="类型">
            <Template Context="data">
                @if(data.FormType == 1)
                {
                <span>收入</span>
                }
                else
                {
                <span>支出</span>
                }
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="Item" Title="项目" />
        <RadzenDataGridColumn Property="Amount" Title="金额" />
        <RadzenDataGridColumn Property="Remark" Title="备注" />
    </Columns>
</RadzenDataGrid>

@code {
    private int value = 0;
    private string keyword = string.Empty;
    private DateTime startDatetime { get; set; } = DateTime.Now;
    private DateTime endDatetime { get; set; } = DateTime.Now;
    ObservableCollection<Finance> data = new ObservableCollection<Finance>();

    private void Changed(object value1)
    {
        var input = new BaseInput()
            {
                keyword = keyword,
                type = (int)value1,
            };
        var list = FinanceService.SearchFinance(input);
        data = new ObservableCollection<Finance>(list);
    }

    private void Search()
    {
        var input = new BaseInput()
        {
             keyword = keyword,
             type = (int)value,
        };
        var list = FinanceService.SearchFinance(input);
        data = new ObservableCollection<Finance>(list);
    }

    private async void Add()
    {
        var f = new Finance();
        Finance result = await DialogService.OpenAsync<FinanceAddDialog>(
            $"添加收支费用",
            new Dictionary<string, object>() { { "PFinance", f } },
            new DefaultDialogOptions());

        // return null when user clicked the cancel button.
        if (result != null)
        {
            data!.Add(result);
        }
    }

    private async void Update(Finance selectedItem)
    {
        Finance result = await DialogService.OpenAsync<FinanceAddDialog>(
            $"修改收支费用",
            new Dictionary<string, object>() { { "PFinance", selectedItem } },
            new DefaultDialogOptions());
        //todo:
        StateHasChanged();
    }

    private async void Delete(Finance selectedItem)
    {
        var result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions()
            {
                OkButtonText = "确定",
                CancelButtonText = "取消"
            });

        if ((bool)result)
        {
            try
            {
                FinanceService.DeleteFinance(selectedItem.Id);
                data!.Remove(selectedItem);
            }
            catch (Exception ex)
            {
                NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = ex.Message });
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        var input = new BaseInput()
            {
                keyword = keyword,
            };
        var list = FinanceService.SearchFinance(input);
        data = new ObservableCollection<Finance>(list);
    }

}

